﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kardex_Entity;
using Kardex_BussinesLogic;
using Kardex_Common;
using KardexSist.Library;
using KardexSist.UserControls.Common;

namespace KardexSist.UserControls.SalePlace
{
    public partial class CtrlPointSale : CtrlMainBase, IOperations
    {
        private sucursal sucursalCurrent;
        private SucursalFactory SucursalFc;

        #region ThrowEvents
        public delegate void ThrowListEventHandler();
        public event ThrowListEventHandler ShowListClick;

        protected virtual void OnShowListClick()
        {
            if (ShowListClick != null)
            { ShowListClick(); }
        }
        #endregion
        public CtrlPointSale()
        {
            InitializeComponent();
            sucursalCurrent = new sucursal();
            if (!this.DesignMode)
            {
                LoadCmb();
            }
        }
        private void LoadCmb()
        {
            try
            {
                TypeBusinessFactory typebusinessActual = new TypeBusinessFactory();
                TypeCreditFactory typecreditActual = new TypeCreditFactory();

                var ListBusiness = typebusinessActual.GetListAll();

                cmbTypeSale.DataSource = ListBusiness;
                cmbTypeSale.DisplayMember = "NameBusiness";

                var ListTypeCredit = typecreditActual.GetListAll();
                cmbTypeCredit.DataSource = ListTypeCredit;
                cmbTypeCredit.DisplayMember = "Days";
            }
            catch (Exception ex)
            {

                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }
        private void CtrlPointSale_Load(object sender, EventArgs e)
        {
            OnShowListClick();
        }
        public void TrigerObjSucursal(sucursal objSucursal)
        {
            sucursalCurrent = new sucursal();            
            sucursalCurrent = objSucursal;
            LoadObjectSucursal();
        }
        private void LoadObjectSucursal()
        {
            try
            {
                txtNameSale.Text = sucursalCurrent.NameBusiness;
                txtNameTax.Text = sucursalCurrent.NameFact;
                cmbTypeSale.SelectedItem = sucursalCurrent.typebusiness;
                txtNitSale.Text = sucursalCurrent.NitBusiness;
                cmbTypeSale.SelectedItem = sucursalCurrent.IdTypeBusiness;
                txtPhone.Text = sucursalCurrent.TelephoneBusiness;
                //dtpFechaInicioTrabajoEmpresa.Value =(DateTime) sucursalCurrent.DateRegistInsert;
                txtDirection.Text = sucursalCurrent.DirectionBusiness;
                txtReferenceSale.Text = sucursalCurrent.RefDirectionBusiness;
                cmbZone.SelectedItem = sucursalCurrent.IdZona;
                cmbRoute.SelectedItem = sucursalCurrent.IdRuta;
                cmbTypeCredit.SelectedItem = sucursalCurrent.typecredit;
                numLatitud.Value = (decimal)sucursalCurrent.GpsLatitud;
                numLongitud.Value = (decimal)sucursalCurrent.GpsLongitud;
                numAltura.Value = (decimal)sucursalCurrent.GpsAltura;
                txtDatum.Text = sucursalCurrent.GpsDatum;

                if (sucursalCurrent.EnableCredit == true)
                {
                    chkHabilidad.Checked = true;
                }
                else
                {
                    chkHabilidad.Checked = false;
                }
            }
            catch (Exception ex)
            {
                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }

        public void LoadData()
        {
            //ClientFactory c = new ClientFactory();
            //var clientactual = c.GetObjByIdperson(85);
            //sucursalCurrent.client = clientactual.client;
            sucursalCurrent.IdPerson = sucursalCurrent.client.IdPerson;//clientactual.client.IdPerson;
            sucursalCurrent.NameBusiness = txtNameSale.Text;
            sucursalCurrent.NameFact = txtNameTax.Text;
            sucursalCurrent.IdTypeBusiness = ((typebusiness)cmbTypeSale.SelectedItem).IdTypeBusiness;
            sucursalCurrent.NitBusiness = txtNitSale.Text;
            sucursalCurrent.TelephoneBusiness = txtPhone.Text;
            sucursalCurrent.DirectionBusiness = txtDirection.Text;
            sucursalCurrent.RefDirectionBusiness = txtReferenceSale.Text;
            sucursalCurrent.IdZona = cmbZone.Text;
            sucursalCurrent.IdRuta = cmbRoute.Text;
            sucursalCurrent.Days = ((typecredit)cmbTypeCredit.SelectedItem).Days;
            sucursalCurrent.GpsLatitud = (float)numLatitud.Value;
            sucursalCurrent.GpsLongitud = (float)numLongitud.Value;
            sucursalCurrent.GpsAltura = (float)numAltura.Value;
            sucursalCurrent.GpsDatum = txtDatum.Text;
            sucursalCurrent.EnableCredit = chkHabilidad.Checked;
            sucursalCurrent.Coment = txtComentario.Text;

        }

        private void ClearData()
        {
            txtNameSale.Text = string.Empty;
            txtNameTax.Text = string.Empty;
            cmbTypeSale.SelectedItem = string.Empty;
            txtNitSale.Text = string.Empty;
            cmbTypeSale.SelectedItem = string.Empty;
            txtPhone.Text = string.Empty;
            //dtpFechaInicioTrabajoEmpresa.Value =(DateTime) sucursalCurrent.DateRegistInsert;
            txtDirection.Text = string.Empty;
            txtReferenceSale.Text = string.Empty;
            cmbZone.SelectedItem = string.Empty;
            cmbRoute.SelectedItem = string.Empty;
            cmbTypeCredit.SelectedItem = string.Empty;
            numLatitud.Value = 0;
            numLongitud.Value = 0;
            numAltura.Value = 0;
            txtDatum.Text = string.Empty;
            chkHabilidad.Checked = false;
        }

        public void Add()
        {
            ClearData();
            sucursalCurrent = new sucursal();
        }

        public void Delete()
        {
            try
            {
                var result = Util.MessageBoxShow(Util.Enum_TypeMessage.QuestionDelete, null);
                if (result == DialogResult.Yes)
                {
                    SucursalFc = new SucursalFactory();
                    if (SucursalFc.Delete(sucursalCurrent) == true)
                    {
                        OnShowListClick();
                        Util.MessageBoxShow(Util.Enum_TypeMessage.Delete, null);
                        ClearData();
                    }
                    else
                    {
                        Util.MessageBoxShow(Util.Enum_TypeMessage.Error, null);
                    }
                }
            }
            catch (Exception ex)
            {
                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }

        public void Save()
        {
            try
            { //insert
                if (sucursalCurrent.IdSucursal <= 0)
                {
                    LoadData();
                    SucursalFc = new SucursalFactory();
                    sucursalCurrent = SucursalFc.Save(sucursalCurrent);
                    if (sucursalCurrent.IdSucursal > 0)
                    {
                        OnShowListClick();
                        Util.MessageBoxShow(Util.Enum_TypeMessage.Insert, null);

                    }
                    else
                    {
                        Util.MessageBoxShow(Util.Enum_TypeMessage.Error, null);
                    }
                }
                else
                {//udpdate
                    var result = Util.MessageBoxShow(Util.Enum_TypeMessage.QuestionModif, null);
                    if (result == DialogResult.Yes)
                    {
                        LoadData();
                        SucursalFc = new SucursalFactory();
                        if (SucursalFc.Update(sucursalCurrent) == true)
                        {
                            OnShowListClick();
                            Util.MessageBoxShow(Util.Enum_TypeMessage.Update, null);
                        }
                        else
                        {
                            Util.MessageBoxShow(Util.Enum_TypeMessage.Error, null);
                        }
                    }
                    else
                    {
                        ClearData();
                        LoadObjectSucursal();
                    }
                }

            }
            catch (Exception ex)
            {
                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }

        public void Export()
        {

        }
    }
}
